package com.example.shengyin.controller;

import com.alibaba.fastjson2.JSON;
import com.example.shengyin.entity.Admin;
import com.example.shengyin.entity.Attach;
import com.example.shengyin.entity.Page;
import com.example.shengyin.entity.Subject;
import com.example.shengyin.service.SubjectService;
import com.example.shengyin.util.Result;
import com.example.shengyin.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * SubjectController
 * 
 * @author gyy
 * @date 2023-07-29
 */
@RestController
@RequestMapping("/subject")
@Validated
@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials="true")
//swagger
@Api("课程")
//redis缓存用
public class SubjectController
{
    @Autowired
    private SubjectService service;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 查询课程列表
     */
    public static final String STAR = "STAR";
    public static final String OMP = "OMP";
    public static final String A2 = "A2";
    public static final String A1 = "A1";
    public static final String B1 = "B1";
    public static final String B2 = "B2";
    public static final String B3 = "B3";
    public static final String B4 = "B4";
    public static final String B5 = "B5";
    public static final String SERVICE = "服务";
    @ApiOperation("课程列表")
    @GetMapping("/list")
    public Result list(Page page,@ApiParam(name = "cname",value = "课程名称") @RequestParam(required = false) String cname,
                       @ApiParam(name = "poster",value = "发布人") @RequestParam(required = false) String poster)
    {

        List<Subject> list = service.list(page,cname,poster);
        int count = service.count(cname,poster);
        if(count==0||list==null)
            return Result.success(page,"没有数据");
        page.setData(list);
        page.setCount(count);
        System.out.println(JSON.toJSONString(Result.success(page)));
        return Result.success(page);
    }
    @GetMapping(value = "/select/{id}")
    public Result select(@PathVariable(value = "id") Long id)
    {
        Subject select = service.select(id);
        if(select==null)
            return Result.error("没有找到数据");
        return Result.success(select);
    }
    @PostMapping("/add")
    public Result insert(@Valid Subject dSubject, BindingResult bindingResult, HttpServletRequest request)
    {
        System.out.println(request.getHeader("Authorization"));
        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        System.out.println("out"+JSON.toJSONString(dSubject));
        dSubject.setPoster(admin.getUname());
        int insert = service.insert(dSubject);
        if(insert==0)
            return Result.error("添加失败");
        return Result.success("添加成功");
    }
    @PostMapping("/edit")
    public Result edit(Subject dSubject)
    {
        int insert = service.update(dSubject);
        if(insert==0)
            return Result.error("修改失败");
        return Result.success("修改成功");
    }

    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable(value = "id")int id)
    {
        int delete = service.delete(id);
        if(delete==0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }
	@GetMapping("/deleteByIds/{ids}")
    public Result deleteByIds(@PathVariable(value = "ids")String ids)
    {
        System.out.println("开始这些");
        String s = StrUtil.ListtoString(ids);
        int delete = service.deleteByIds(s);
        if(delete==0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }
    @ApiOperation("显示")
    @GetMapping("/isvalue/{id}/{isvalue}")
    public Result edit(@PathVariable(value = "id") Long id,
                       @PathVariable(value = "isvalue")boolean isvalue,HttpServletRequest request)
    {
        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        Subject attach=new Subject();
        System.out.println(id);
        attach.setId(id);
        System.out.println(isvalue);
        attach.setIsvalid(isvalue);
        attach.setPoster(admin.getRealname());
        attach.setPostertime(new Date());
        int insert = service.update(attach);

        if(insert==0)
            return Result.error("修改失败");
        return Result.success("修改成功");
    }
    @GetMapping("/getAll")
    @ApiOperation("根据id查找课程信息")
    public Result getAll(long id){
        List<Subject> all = service.getAll(id);
        return all==null?Result.error("无"):Result.success(all);
    }
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));

    }
}
